package com.igame.mapper;

import com.igame.dto.ChangePasswordDto;
import com.igame.dto.FollowDto;
import com.igame.entity.AreaInfo;
import com.igame.entity.Follow;
import com.igame.entity.PasswordSecurity;
import com.igame.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Insert("INSERT INTO user(username, `password`, email, age, birth, address, state, avatar_path, `role`,create_time) " +
            "VALUES (#{username}, #{password}, #{email}, #{age}, #{birth}, #{address}, #{state},#{AvatarPath},#{role},#{createTime})")
    int insert(User user);

    @Select("SELECT * FROM user WHERE LOWER(email) = LOWER(#{email})")
    User findUserByEmail(@Param("email") String email);

    @Select("SELECT * FROM user WHERE username = #{username}")
    User selectByUserName(@Param("username") String username);

    @Select("select avatar_path from user where username=#{username}")
    String selectAvatarByUserName(@Param("username") String username);

    @Insert("INSERT INTO follow(id, user_id, follow_user) VALUES (null,#{userId},#{followUser})")
    int insertFollow (Follow follow);

    @Select("SELECT * from follow where user_id=#{userId} and follow_user=#{followUser}")
    Follow isFollow (FollowDto followDto);

    @Select("SELECT id from user where username =#{userName}")
    Integer selectByIdName(@Param("userName") String userName);

    @Select("SELECT username from user where id=#{id}")
    String selectUserNameById(@Param("id")Integer id);

    @Select("SELECT * from user where id=#{id}")
    User selectUserById(Integer id);

    @Select("SELECT * from areainfo")
    List<AreaInfo> getAllArea();

    void updateUser(User user);

    @Select("SELECT `username` from user where id!=#{id} and username=#{username}")
    String userNameIsExist(Integer id,String username);

    @Select("SELECT `username` from user where id=#{id}")
    String getNameById(Integer id);

    @Insert("INSERT INTO password_security(id, user_id, question, answer)values(#{id},#{userId},#{question},#{answer})")
    int insertPasswordSecurity(PasswordSecurity passwordSecurity);

    @Select("select * from password_security where user_id=#{userId}")
    List<PasswordSecurity> userHavePds(Integer userId);

    @Update("UPDATE user set `password` =#{newPassword} where id =#{id}")
    int updatePassword(ChangePasswordDto changePasswordDto);

    @Update("UPDATE user set `password` =#{newPassword} where email =#{email}")
    int updatePasswordByEmail(ChangePasswordDto changePasswordDto);

    @Update("UPDATE user set `password` =#{newPassword} where username =#{username}")
    int updatePasswordByUsername(ChangePasswordDto changePasswordDto);

}
